package com.jycrm.dao.serve.impl;

import java.io.Serializable;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateCallback;

import com.jycrm.common.PagedListHolder;
import com.jycrm.dao.core.AbstractService;
import com.jycrm.dao.serve.ICrmServiceDocDetailDao;
import com.jycrm.domain.serve.CrmServiceDocDetail;
import com.jycrm.service.product.vo.SeachCrmServiceDocDetailParamVO;
import com.jycrm.util.DataDictConstantValue;
import com.jycrm.util.StringUtil;

public class CrmServiceDocDetailDaoImpl extends AbstractService implements
		ICrmServiceDocDetailDao {
	public void save(final CrmServiceDocDetail detail) {
		this.queryManager.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				Serializable rt = session.save(detail);
				session.flush();
				session.refresh(detail);
				return rt;
			}
		}		 
		);

	}

	public void update(final CrmServiceDocDetail detail) {
		
			this.queryManager.execute(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException {
					  session.update(detail);
					session.flush();
					session.refresh(detail);
					return null;
				}
			}		 
			);

	}

	public void delete(CrmServiceDocDetail detail) {
		persistenceManager.delete(detail);

	}

	public CrmServiceDocDetail getCrmServiceDocDetailByID(Long id) {
		return (CrmServiceDocDetail) queryManager.getObjectById(
				CrmServiceDocDetail.class, id);
	}

	public PagedListHolder getCrmServiceDocDetailPagedListHolder(final SeachCrmServiceDocDetailParamVO param,final int pageSize,final int pageNo) {
		 
		PagedListHolder p = (PagedListHolder) this.queryManager.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				 
				StringBuffer queryStr = new StringBuffer(
						"FROM CrmServiceDocDetail as d where d.deleted.code <> "+ DataDictConstantValue.DATADICT_GENERAL_YES_CODE);
				
				if (param.getCommitDateBegin() != null) {
					queryStr.append(" and d.commitDate >= :commitDateBegin  ");

				}
				if (param.getCommitDateEnd() != null) {
					queryStr.append("  and d.commitDate <= :commitDateEnd ");

				}
				if (!StringUtil.isStrEmpty(param.getName())) {
					queryStr.append("and d.name like :name ");

				}
				if (param.getServerMethod() != null) {
					queryStr.append(" and d.service.serverMethod = :serverMethod ");

				}
				if (param.getCreater() != null) {
					queryStr.append(" and d.creater.id = :creater ");

				}
				if (param.getExaminer() != null) {
					queryStr.append(" and d.examiner.id = :examiner ");

				}
				if (param.getStatus() != null) {
					queryStr.append(" and d.status = :status ");

				}
				
				queryStr.append( " order by d.commitDate desc ");
				
				
				StringBuffer queryCount = new StringBuffer("SELECT COUNT(*) " +queryStr.toString());				
				Query query1 = session.createQuery(queryCount.toString());
				query1.setProperties(param);
				int total  = ((Integer) query1.iterate().next() ).intValue();
				PagedListHolder pagedListHolder = new PagedListHolder();
				pagedListHolder.setPageSize(pageSize);
				pagedListHolder.setTotal(total);
				pagedListHolder.setPage(pageNo);
				
				
				Query query = session.createQuery(queryStr.toString());
				query.setProperties(param);
				query.setMaxResults(pagedListHolder.getPageSize());
				query.setFirstResult(pagedListHolder.getPage()*pagedListHolder.getPageSize());
				List rs = query.list();
				pagedListHolder.setPageList(rs);
				return pagedListHolder;
			}

		});
		return p;
	}
    
	public List getUnSendsCrmServiceDocDetailList(final String serviceMode) {
		List rs = this.queryManager.findPagination(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException {
				StringBuffer queryStr = new StringBuffer(
						"FROM CrmServiceDocDetail as d where d.deleted.code <> "+ DataDictConstantValue.DATADICT_GENERAL_YES_CODE);
				queryStr.append(" and d.analysised = " + DataDictConstantValue.DATADICT_GENERAL_NO_CODE);
				queryStr.append(" and d.status.code = " + DataDictConstantValue.SERVER_DETAIL_STATUS_PASS);
				queryStr.append(" and d.service.serverMethod.code = " + serviceMode);
				Query query = session.createQuery(queryStr.toString());
				query.setMaxResults(5);
				query.setFirstResult(0);
				
				return query.list();
			}
			});
		return rs;
	}
	
	
	
	public List getCreaterList() {
		return queryManager.findByNamedQuery("getCreaterList",null);
	}

	public List getExaminerList() {
		return queryManager.findByNamedQuery("getExaminerList",null);
	}

	
}
